package tommysmapeditor.editors.widget;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import tommysmapeditor.editors.AnimationAndHitboxEditor;
import tommysmapeditor.editors.bo.FrameDescription;
import tommysmapeditor.editors.bo.HitBoxDescription;

public class AnimFrameCanvas extends Canvas {

	Color hbCol;
	
	public FrameDescription _current;
	
	/**
	 * says wether a click signifies
	 * -- I become the active editor
	 * -- hitbox def
	 */
public	boolean currentEdited;
	
	boolean firstHitboxClick=true; // for hitbox def
	
	Point firstClickLoc;
	Point secondClickLoc;
	
	AnimationAndHitboxEditor _editor;
	
	/**
	 * at a given time we only edit one hitbox
	 */
	public HitBoxDescription currentHitbox =null;
	
	public FrameDescription getFrameDescription() {
		return _current;
	}

	public AnimFrameCanvas(
			Composite parent, 
			int style,
			FrameDescription fdesc, 
			int initialY,
			final AnimationAndHitboxEditor editor,
			int zoom,
			int editMargin
			) {
		super(parent, style);

		_editor = editor;
		
		hbCol=Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
		
		_current = fdesc;
		
		addPaintListener();
		sizeCanvasFromZoom(
				fdesc, 
				initialY,
				zoom,
				editMargin);

		addMouseListener(new MouseListener(){

			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void mouseDown(MouseEvent e) {
				
				if( !currentEdited ){
					System.out.println("I become the current edited");
					//notifying parent for house keeping
					editor.newCanvasEdit(AnimFrameCanvas.this);
					return;
					
				}
				
				
				if(firstHitboxClick){
					firstHitboxClick=!firstHitboxClick;
					System.out.println("first");
					firstClickLoc = new Point(
							e.x/_editor.zoomFactor-_editor.frameMargin,
							e.y/_editor.zoomFactor-_editor.frameMargin
							);
					
				}else{
					firstHitboxClick=!firstHitboxClick;
					System.out.println("second");
					secondClickLoc = new Point(
							e.x/_editor.zoomFactor-_editor.frameMargin,
							e.y/_editor.zoomFactor-_editor.frameMargin);
					
					
					currentHitbox.coordinates=new Rectangle(firstClickLoc.x, firstClickLoc.y, secondClickLoc.x-firstClickLoc.x, secondClickLoc.y-firstClickLoc.y);
					
					System.out.println("hitbox: "+currentHitbox.coordinates.x+ " "+currentHitbox.coordinates.y+"  "+currentHitbox.coordinates.width+" "+currentHitbox.coordinates.height);
					AnimFrameCanvas.this.redraw();
				}
			}

			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}

	private void sizeCanvasFromZoom(FrameDescription fdesc, int initialY, int zoom, int editMargin) {
		setBounds(10, initialY,
				(editMargin*2+fdesc.base.getImageData().width)*zoom,
				(editMargin*2+fdesc.base.getImageData().height)*zoom
				);
	}

	private void addPaintListener() {
		addPaintListener(new PaintListener(){

			public void paintControl(PaintEvent e) {
				System.out.println("paint");
				
				
				switch(_editor.zoomFactor){
				case 1:
				e.gc.drawImage(_current.base, _editor.frameMargin, _editor.frameMargin);
				break;
				case 2:
					e.gc.drawImage(_current.baseX2, _editor.frameMargin*2, _editor.frameMargin*2);
					break;
				case 3:
					e.gc.drawImage(_current.baseX3, _editor.frameMargin*3, _editor.frameMargin*3);
					break;
				
				}
				
				for(HitBoxDescription toDraw : getFrameDescription().hitBoxes){
					System.out.println("drawing rectangle");
					e.gc.setForeground(hbCol);
					e.gc.drawRectangle(
							new Rectangle(
									(_editor.frameMargin+toDraw.coordinates.x)*_editor.zoomFactor, 
									(_editor.frameMargin+toDraw.coordinates.y)*_editor.zoomFactor, 
									(toDraw.coordinates.width)*_editor.zoomFactor, 
									(toDraw.coordinates.height)*_editor.zoomFactor
									
							)
							
							);
					e.gc.drawLine(
							(_editor.frameMargin+toDraw.coordinates.x)*_editor.zoomFactor, 
							(_editor.frameMargin+toDraw.coordinates.y)*_editor.zoomFactor, 
							(_editor.frameMargin+toDraw.coordinates.x+toDraw.coordinates.width)*_editor.zoomFactor, 
							(_editor.frameMargin+toDraw.coordinates.y+toDraw.coordinates.height)*_editor.zoomFactor
							);
				}

					
				if(currentEdited){
					e.gc.setForeground(hbCol);
					e.gc.drawRectangle(0,0,getBounds().width-1,getBounds().height-1);
					
				}
			}
			
		});
	}

	/**
	 * to position next canvas
	 * @return
	 */
	public int getLowerY(){
		int ret = getBounds().y + getBounds().height;
		return ret;
		
	}

	/** 
	 * on zoom change bounds to be changed
	 * @param zoomFactor
	 */
	public void resizeCanvas(int initialY,int zoom,int editMargin) {
		sizeCanvasFromZoom(_current, initialY, zoom,editMargin);
	}

	
	/**
	 * setting the edited hitbox
	 * @param selection
	 */
	public void selectCurrentlyEdited(String selection) {
		for(HitBoxDescription hbd : getFrameDescription().hitBoxes){
			if(hbd.name.equals(selection)){
				currentHitbox = hbd;
			}
		}
		
	}
	
}
